package com.ls.server.controller;

import com.ls.server.dto.ChatMessageDTO;
import com.ls.server.dto.ChatSessionDTO;
import com.ls.server.service.ChatAttachmentService;
import com.ls.server.service.ChatMessageService;
import com.ls.server.service.ChatModelService;
import com.ls.server.service.ChatSessionService;
import com.ls.server.vo.ChatAttachmentVO;
import com.ls.server.vo.ChatMessageVO;
import com.ls.server.vo.ChatModelVO;
import com.ls.server.vo.ChatSessionVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 聊天服务接口
 */
@Slf4j
@RestController
@RequestMapping("/chat")
@RequiredArgsConstructor
public class ChatController {

    private final ChatModelService modelService;
    private final ChatSessionService sessionService;
    private final ChatMessageService messageService;
    private final ChatAttachmentService attachmentService;

    /**
     * 获取可用的AI模型列表
     */
    @GetMapping("/models")
    public ResponseEntity<List<ChatModelVO>> getEnabledModels() {
        return ResponseEntity.ok(modelService.listEnabledModels());
    }

    /**
     * 获取用户的聊天会话列表
     */
    @GetMapping("/sessions")
    public ResponseEntity<List<ChatSessionVO>> getUserSessions(@RequestParam Long userId) {
        return ResponseEntity.ok(sessionService.listUserSessions(userId));
    }

    /**
     * 创建聊天会话
     */
    @PostMapping("/sessions")
    public ResponseEntity<String> createSession(@RequestBody ChatSessionDTO dto) {
        return ResponseEntity.ok(sessionService.createSession(dto));
    }

    /**
     * 更新聊天会话信息
     */
    @PutMapping("/sessions/{sessionId}")
    public ResponseEntity<Void> updateSession(@PathVariable String sessionId, @RequestBody ChatSessionDTO dto) {
        dto.setSessionId(sessionId);
        sessionService.updateSession(dto);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除聊天会话
     */
    @DeleteMapping("/sessions/{sessionId}")
    public ResponseEntity<Void> deleteSession(@PathVariable String sessionId) {
        sessionService.deleteSession(sessionId);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取会话的消息列表
     */
    @GetMapping("/sessions/{sessionId}/messages")
    public ResponseEntity<List<ChatMessageVO>> getSessionMessages(@PathVariable String sessionId) {
        return ResponseEntity.ok(messageService.listSessionMessages(sessionId));
    }

    /**
     * 发送消息
     */
    @PostMapping("/messages")
    public ResponseEntity<Long> sendMessage(@RequestBody ChatMessageDTO dto) {
        return ResponseEntity.ok(messageService.sendMessage(dto));
    }

    /**
     * 上传附件
     */
    @PostMapping("/attachments")
    public ResponseEntity<Long> uploadAttachment(
            @RequestParam("file") MultipartFile file,
            @RequestParam("sessionId") String sessionId,
            @RequestParam("userId") Long userId) {
        return ResponseEntity.ok(attachmentService.uploadAttachment(file, sessionId, userId));
    }

    /**
     * 获取消息的附件列表
     */
    @GetMapping("/messages/{messageId}/attachments")
    public ResponseEntity<List<ChatAttachmentVO>> getMessageAttachments(@PathVariable Long messageId) {
        return ResponseEntity.ok(attachmentService.listByMessageId(messageId));
    }
} 